package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.text.Html
import android.text.TextPaint
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import cn.hl.popmenu.ActionItem
import cn.hl.popmenu.PopMenu
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReplaceQues.AibkTestPaperReplaceQuesActivity
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewDXT.QuesReviewFragmentDxt
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewFHXZ.QuesReviewFragmentFhxz
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewGCT.QuesReviewFragmentGct
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewJDT.QuesReviewFragmentJdt
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewPPT.QuesReviewFragmentPpt
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewTKT.QuesReviewFragmentTkt
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewWXXZ.QuesReviewFragmentWxxz
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewXZT.QuesReviewFragmentXzt
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewYDPP.QuesReviewFragmentYdpp
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewZHT.QuesReviewFragmentZht
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewZW.QuesReviewFragmentZw
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkDialog.ShowDialogTools
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkPublicActivity.ActivityErrorRecovery
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.MyRefreshFragmentPagerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.*
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_activity_paper_preview.*
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.*
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.LlToolsView
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.SBtnAnswerSheet
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.SBtnBack
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.SBtnErrorRecovery
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.TvPaperName
import kotlinx.android.synthetic.main.aibk_activity_testpaper_review_question.TvQuesInfor
import kotlinx.android.synthetic.main.aibk_include_loading_refresh.*
import org.koin.androidx.viewmodel.ext.android.viewModel

class AibkTestPaperReviewActvity() : KBaseActivity(), View.OnClickListener {

    private var layer = 0
    private var paperId = ""
    private var stuId = ""
    companion object {
        fun launch(context: Context, layer:Int,paperId: String, stuId: String) {
            val intent = Intent(context, AibkTestPaperReviewActvity::class.java)
            intent.putExtra("Layer", layer)//0语法能力  1语言能力
            intent.putExtra("PaperID", paperId)
            intent.putExtra("StuID", stuId)
            context.startActivity(intent)
        }
    }

    private var quesScoreInfor = ""
    private var isFullOneLine = false
    private var typeListPosition = 0
    private var quesListPosition = 0
    private var vpQuesPosition = 0
    private val mViewModel: MarkViewModel by viewModel()
    private var testPaperReviewInfor: TestPaperReviewInfor?=null
    private var fragmentList: MutableList<Fragment>? = ArrayList()

    override fun setLayout(): Int {
        return R.layout.aibk_activity_testpaper_review_question
    }

    override fun initView() {
        SBtnBack.setOnClickListener(this)
        SBtnErrorRecovery.setOnClickListener(this)
        SBtnReplaceQues.setOnClickListener(this)
        SBtnAnswerSheet.setOnClickListener(this)
        LlToolsView.visibility = View.GONE
        TvQuesInfor.visibility = View.GONE
        TvQuesInfor.setOnClickListener(this)

        initPopMenu()
    }

    override fun initData() {
        layer = intent.getIntExtra("Layer",0)
        paperId = intent.getStringExtra("PaperID")
        stuId = intent.getStringExtra("StuID")
        mViewModel.getReviewPaperInfo_1v1(paperId, stuId)
                .doOnSubscribe {
                    RlGifLoading.visibility = View.VISIBLE
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        RlGifLoading.visibility = View.GONE
                        testPaperReviewInfor = it.data

                        showPreviewPaperView(testPaperReviewInfor!!)

                    } else {
                        MyToast.showToast(it.message)
                        Handler().postDelayed({
                            finish()
                        }, 500)
                    }
                }, {
                    MyToast.showToast(it.message)
                    Handler().postDelayed({
                        finish()
                    }, 500)
                })
    }

    private fun showPreviewPaperView(testPaperReviewInfor: TestPaperReviewInfor) {
        //显示试卷名称以及答题卡按钮
        LlToolsView.visibility = View.VISIBLE
        TvPaperName.text = "试卷审核"
        TvQuesInfor.visibility = View.VISIBLE

        //加载试题,显示试卷作答情况
        fragmentList?.clear()
        var typeListSize = testPaperReviewInfor.TypeList.size
        for (i in 0 until typeListSize) {
            var typeNo = testPaperReviewInfor.TypeList[i].TypeNo
            var quesListSize = testPaperReviewInfor.TypeList[i].QuesList.size
            for (j in 0 until quesListSize) {
                if (typeNo == "A") {
                    val mFragment = QuesReviewFragmentDxt(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "C") {
                    val mFragment = QuesReviewFragmentWxxz(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F" || typeNo == "D") {//听力选择，听力判断，阅读选择
                    val mFragment = QuesReviewFragmentXzt(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "k") {
                    val mFragment = QuesReviewFragmentZht(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                }else if (typeNo == "H") {
                    val mFragment = QuesReviewFragmentFhxz(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                }  else if (typeNo == "f") {
                    val mFragment = QuesReviewFragmentZw(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "N" || typeNo == "S") { //N:普通填空题  S：听力填空题
                    val mFragment = QuesReviewFragmentTkt(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "Q" || typeNo == "b" || typeNo == "d"
                        || typeNo == "X" || typeNo == "g" || typeNo == "V"
                        || typeNo == "G") {//Q:阅读填空,X:阅读简答,G:听力简答,
                    val mFragment = QuesReviewFragmentJdt(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "x" || typeNo == "m") {//x:听力匹配,m:普通匹配题
                    val mFragment = QuesReviewFragmentPpt(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "U") {
                    val mFragment = QuesReviewFragmentGct(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "P") {
                    val mFragment = QuesReviewFragmentYdpp(this, testPaperReviewInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                }
            }
        }

        val mAdapter = MyRefreshFragmentPagerAdapter(supportFragmentManager, fragmentList as ArrayList<Fragment>)
        VpReviewQues.adapter = mAdapter

        //处理viewpager滑动事件
        VpReviewQues.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                vpQuesPosition = position
                showQuesTitleInfor(testPaperReviewInfor)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })

        showQuesTitleInfor(testPaperReviewInfor)
    }

    private fun showQuesTitleInfor(testPaperReviewInfor: TestPaperReviewInfor) {
        typeListPosition = QuesReviewInforIndexHelp.QuesPosition_return_TypeListPosition(testPaperReviewInfor, vpQuesPosition)

        val typeIndex = testPaperReviewInfor.TypeList[typeListPosition].TypeIndex
        val strTypeIndex = LancooMethod.numberToChinese(typeIndex)
        var genreName = testPaperReviewInfor.TypeList[typeListPosition].GenreName
        val typeName = testPaperReviewInfor.TypeList[typeListPosition].TypeName
        if (genreName.isEmpty()) {
            genreName = typeName
        }
        /******************************************************************************************/
        var totalChildQuesSize = 0
        var totalChildQuesScore = 0.0
        val quesListSize = testPaperReviewInfor.TypeList[typeListPosition].QuesList.size
        for (i in 0 until quesListSize) {
            val sortIndexListSize = testPaperReviewInfor.TypeList[typeListPosition].QuesList[i].SortIndexList.size
            totalChildQuesSize += sortIndexListSize

            for (j in 0 until sortIndexListSize) {
                val score = testPaperReviewInfor.TypeList[typeListPosition].QuesList[i].SortIndexList[j].Score
                totalChildQuesScore += score
            }
        }

        var isAverage = true
        val averageScore = LancooMethod.getCorrectRateInt(totalChildQuesScore, totalChildQuesSize)
        outLoop@ for (i in 0 until quesListSize) {
            val sortIndexListSize = testPaperReviewInfor.TypeList[typeListPosition].QuesList[i].SortIndexList.size
            for (j in 0 until sortIndexListSize) {
                val score = testPaperReviewInfor.TypeList[typeListPosition].QuesList[i].SortIndexList[j].Score
                if (score != averageScore) {
                    isAverage = false
                    break@outLoop
                }
            }
        }

            if (isAverage) {
                isFullOneLine = false
                val strAverageScore = LancooMethod.killlingDouble(averageScore)
                TvQuesInfor.text = Html.fromHtml(resources.getString(R.string.aibk_practice_ques_infor_short,
                        strTypeIndex, genreName, quesListSize,totalChildQuesSize, strAverageScore))
            } else {
                isFullOneLine = true
                var childScoreAll = ""
                val quesListSize = testPaperReviewInfor.TypeList[typeListPosition].QuesList.size
                for (x in 0 until quesListSize) {
                    val sortIndexListSize = testPaperReviewInfor.TypeList[typeListPosition].QuesList[x].SortIndexList.size
                    for (y in 0 until sortIndexListSize) {
                        val sortIndex = testPaperReviewInfor.TypeList[typeListPosition].QuesList[x].SortIndexList[y].SortIndex
                        val score = testPaperReviewInfor.TypeList[typeListPosition].QuesList[x].SortIndexList[y].Score
                        val strScore = LancooMethod.killlingDouble(score)
                        val childScore = resources.getString(R.string.aibk_single_ques_infor, sortIndex, strScore)
                        if (childScoreAll.isEmpty()) {
                            childScoreAll = childScore
                        } else {
                            childScoreAll = "$childScoreAll,$childScore"
                        }
                    }
                }
                TvQuesInfor.text = Html.fromHtml(resources.getString(R.string.aibk_practice_ques_infor_long,
                        strTypeIndex, genreName,quesListSize, totalChildQuesSize, childScoreAll))
            }
        /******************************************************************************************/
    }


    //实现答题卡弹窗
    private fun showAnswerSheetDialog() {
        val dialog = AibkTestPaperReviewAnswerSheetDialog(this, this, testPaperReviewInfor!!, typeListPosition)
        dialog.show()
        dialog.setClickDialogListener(object : AibkTestPaperReviewAnswerSheetDialog.OnClickDialogListener {

            override fun onSortIndexClick(sortIndex: String) {
                dialog.dismiss()
                var parentVpPosition = QuesReviewInforIndexHelp.SortIndex_return_ParentVpPosition(testPaperReviewInfor!!, sortIndex)
                VpReviewQues.setCurrentItem(parentVpPosition, true)

                var index_A = QuesReviewInforIndexHelp.SortIndex_return_IndexA(testPaperReviewInfor!!, sortIndex)
                var typeNo = testPaperReviewInfor!!.TypeList[index_A].TypeNo
                if (typeNo == "k") {//k：阅读综合
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentZht
                    fragment.jumpToSortIndex(sortIndex)
                } else if (typeNo == "x" || typeNo == "m") {//x:听力匹配,m:普通匹配题
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentPpt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F" || typeNo == "D") {//听力选择，听力判断，阅读选择
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentXzt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "Q" || typeNo == "b" || typeNo == "d"
                        || typeNo == "X" || typeNo == "g" || typeNo == "V"
                        || typeNo == "G") {//Q:阅读填空,X:阅读简答,G:听力简答,
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentJdt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "H") {//H:复合写作
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentFhxz
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "C") {//C:完形选择
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentWxxz
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "N" || typeNo == "S") { //N:普通填空题  S：听力填空题
                    val fragment = fragmentList!![parentVpPosition] as QuesReviewFragmentTkt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                }

            }

            override fun onDismissClick() {
                dialog.dismiss()
            }

            override fun onPutOutPaper() {
//                MyToast.showToast("发布试卷")
                publishPaper(dialog)
            }

            override fun onGiveUpPaper() {
//                MyToast.showToast("放弃试卷")
                deletePaper(dialog)
            }

        })
    }

    override fun onPause() {
        super.onPause()
        toolPauseAudio()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.SBtnBack -> {
                toolPauseAudio()
                finish()
            }
            R.id.SBtnAnswerSheet -> {
                toolPauseAudio()
                showAnswerSheetDialog()
            }
            R.id.SBtnReplaceQues ->{
                val isThemeExist = testPaperReviewInfor!!.TypeList[typeListPosition].IsThemeExist
                if(isThemeExist){
                    //弹出试题替换和主题替换选择弹窗
                    quesListPosition = QuesReviewInforIndexHelp.QuesPosition_return_QuesListPosition(testPaperReviewInfor!!, vpQuesPosition)
                    var theme = testPaperReviewInfor!!.TypeList[typeListPosition].QuesDetailList[quesListPosition].Theme
                    showPopMenu(mPopMenu!!,SBtnReplaceQues, theme)
                }else{
                    //直接默认进入试题替换页面
                    jumpToRepalceQuesActivity(false,"")
                }
            }
            R.id.SBtnErrorRecovery -> {
                toolPauseAudio()
                quesListPosition = QuesReviewInforIndexHelp.QuesPosition_return_QuesListPosition(testPaperReviewInfor!!, vpQuesPosition)
                val quesId = testPaperReviewInfor!!.TypeList[typeListPosition].QuesList[quesListPosition].QuesID
                ActivityErrorRecovery.launch(this,quesId)
            }
            R.id.TvQuesInfor ->{
                quesScoreInfor =  TvQuesInfor.text.toString()
                val mTextPaint: TextPaint = TvQuesInfor.paint
                mTextPaint.textSize = TvQuesInfor.textSize
                val mTextViewWidth = mTextPaint.measureText(quesScoreInfor).toInt()
                val mTvQuesInforWidth = TvQuesInfor.width - DensityUtils.dp2px(this,20f)
                if (mTextViewWidth > mTvQuesInforWidth) { //超出一行
                    ShowDialogTools.showQuesScoreInofrDialog(this, quesScoreInfor)
                }
            }
        }
    }

    /*
  * 获取纠错类型
  * */
    fun getErrorConfig() {
        mViewModel.getErrorConfig()
                .doOnSubscribe {
                }.bindLifeCycle(this)
                .subscribe({
                    configListener!!.getErrorRecoverySuccess(it.data)
                }, {
                    configListener!!.getErrorRecoveryFaile(it.message.toString())
                })
    }

    interface OnConfigListener {
        fun getErrorRecoverySuccess(errorRecovery: List<ErrorRecovery>)
        fun getErrorRecoveryFaile(errorMessage: String)
    }

    private var configListener: OnConfigListener? = null

    fun setConfigListener(configListener: OnConfigListener?) {
        this.configListener = configListener
    }

    //暂停一切音频播放
    private fun toolPauseAudio() {
        if (testPaperReviewInfor != null) {
            var typeNo = testPaperReviewInfor!!.TypeList[typeListPosition].TypeNo
            if (typeNo == "x") {//x:听力匹配
                val fragment = fragmentList!![vpQuesPosition] as QuesReviewFragmentPpt
                fragment.pauseAudio()
                fragment.pauseChildAudio()
                fragment.audioTrackerStop()
            } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F") {//听力选择，听力判断
                val fragment = fragmentList!![vpQuesPosition] as QuesReviewFragmentXzt
                fragment.pauseAudio()
                fragment.pauseChildAudio()
                fragment.audioTrackerStop()
            } else if (typeNo == "G") {//G:听力简答,
                val fragment = fragmentList!![vpQuesPosition] as QuesReviewFragmentJdt
                fragment.pauseAudio()
                fragment.pauseChildAudio()
                fragment.audioTrackerStop()
            } else if (typeNo == "S") { // S：听力填空题
                val fragment = fragmentList!![vpQuesPosition] as QuesReviewFragmentTkt
                fragment.pauseAudio()
                fragment.pauseChildAudio()
                fragment.audioTrackerStop()
            } else if (typeNo == "f") {
                val fragment = fragmentList!![vpQuesPosition] as QuesReviewFragmentZw
                fragment.pauseAudio()
                fragment.pauseChildAudio()
                fragment.audioTrackerStop()
            } else if (typeNo == "H") {
                val fragment = fragmentList!![vpQuesPosition] as QuesReviewFragmentFhxz
                fragment.pauseAudio()
                fragment.pauseChildAudio()
                fragment.audioTrackerStop()
            }
        }
    }

    private var mPopMenu: PopMenu? = null
    private fun initPopMenu() {
        val mPopItems: MutableList<ActionItem> = ArrayList()
        mPopItems.add(ActionItem(0, "智能替换"))
        mPopItems.add(ActionItem(1, "主题搜索"))
        mPopMenu = PopMenu.Builder(this)
                .addData(mPopItems) //.setCancelable(false)
                .setCornerRadius(10f)
                .build()
    }

    private fun showPopMenu(popMenu: PopMenu, v: View, theme: String) {
        if (popMenu.isShowing) {
            popMenu.dismiss()
        } else {
            popMenu.show(v)
            popMenu.setOnItemClickListener { item, position ->
                when (item.tag) {
                    0 -> {
//                        MyToast.showToast(item.text)
                        jumpToRepalceQuesActivity(false,"")
                    }
                    1 -> {
//                        MyToast.showToast(item.text)
                        jumpToRepalceQuesActivity(true,theme)
                    }

                }
            }
        }
    }

    /*
    * 获取请求替换试题的入参body
    * */
    private fun jumpToRepalceQuesActivity(isTheme:Boolean,theme:String){
        quesListPosition = QuesReviewInforIndexHelp.QuesPosition_return_QuesListPosition(testPaperReviewInfor!!, vpQuesPosition)
        val quesId = testPaperReviewInfor!!.TypeList[typeListPosition].QuesList[quesListPosition].QuesID
        val paperId = testPaperReviewInfor!!.PaperID
        val paperType = testPaperReviewInfor!!.PaperType
        val typeNo = testPaperReviewInfor!!.TypeList[typeListPosition].TypeNo
        val genreId = testPaperReviewInfor!!.TypeList[typeListPosition].GenreID
        val specialFeature = testPaperReviewInfor!!.TypeList[typeListPosition].QuesDetailList[quesListPosition].SpecialFeatureCodes
        val itemNum = testPaperReviewInfor!!.TypeList[typeListPosition].QuesDetailList[quesListPosition].QuesChildNum
        var mFilterId = ""  //mFilterId有待商榷，TypeList下的某个题型，自定义模板中相同题型下不同组的QuesID是否也需要过滤，如何筛选
        val typeListSize = testPaperReviewInfor!!.TypeList.size
        for(i in 0 until typeListSize){
            val typeNoX = testPaperReviewInfor!!.TypeList[i].TypeNo
            val quesDetailListSize = testPaperReviewInfor!!.TypeList[i].QuesDetailList.size
            for(j in 0 until quesDetailListSize) {
                if (typeNo == typeNoX) {
                    val quesId = testPaperReviewInfor!!.TypeList[i].QuesDetailList[j].QuesID
                    if(mFilterId.isEmpty()){
                        mFilterId = quesId
                    }else{
                        mFilterId = "$mFilterId|$quesId"
                    }
                }
            }
        }
        val sFilterId = ""
        val importKlgList:List<ImportKlgReview> = testPaperReviewInfor!!.TypeList[typeListPosition].QuesDetailList[quesListPosition].ImportKlgList
        val mainKlgList:List<MainKlgReview> = testPaperReviewInfor!!.TypeList[typeListPosition].QuesDetailList[quesListPosition].MainKlgList
        val startIndex = testPaperReviewInfor!!.TypeList[typeListPosition].QuesDetailList[quesListPosition].ChildList[0].ItemList[0].SortIndex


        val importKlgListX = ArrayList<String>()
        for (i in importKlgList.indices) {
            importKlgListX.add(importKlgList[i].UniqueCode)
        }
        val mainKlgListX = ArrayList<String>()
        for (i in mainKlgList.indices) {
            mainKlgListX.add(mainKlgList[i].UniqueCode)
        }

        AibkTestPaperReplaceQuesActivity.launch(this,quesId,paperId,paperType,stuId,typeNo,genreId,specialFeature,itemNum,mFilterId,sFilterId,
                importKlgListX,mainKlgListX,isTheme,theme,startIndex.toInt())
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            if (resultCode == 303) {
                mViewModel.getReviewPaperInfo_1v1(paperId, stuId)
                        .doOnSubscribe {
                            RlGifLoading.visibility = View.VISIBLE
                        }.bindLifeCycle(this)
                        .subscribe({
                            if (it.code == 1) {
                                RlGifLoading.visibility = View.GONE
                                testPaperReviewInfor = it.data

                                showPreviewPaperView(testPaperReviewInfor!!)

                                VpReviewQues.currentItem = vpQuesPosition

                            } else {
                                MyToast.showToast(it.message)
                            }
                        }, {
                            MyToast.showToast(it.message)
                        })
            }
        }
    }

    /*
    * 发布试卷
    * */
    private fun publishPaper(dialog:AibkTestPaperReviewAnswerSheetDialog) {
        mViewModel.publishPaper(paperId,
                stuId, layer)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        //发布试卷成功
                        MyToast.showToast(it.message)
                        dialog.dismiss()
                        finish()
                    } else {
                        MyToast.showToast(it.message)
                    }
                    stopLoad()
                }, {
                    MyToast.showToast(it.message)
                    stopLoad()
                })
    }

    /*
    * 删除未发布的试卷
    * */
    private fun deletePaper(dialog:AibkTestPaperReviewAnswerSheetDialog) {
        mViewModel.delPaper(paperId,
                stuId, layer)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        //放弃组卷成功
                        MyToast.showToast(it.message)
                        dialog.dismiss()
                        finish()
                    } else {
                        MyToast.showToast(it.message)
                    }
                    stopLoad()
                }, {
                    MyToast.showToast(it.message)
                    stopLoad()
                })
    }

}